package woda.ink.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import woda.ink.entity.fileEntity;
import woda.ink.entity.loginEntity;
import woda.ink.mapper.Commom;

import java.util.List;

@Service
public class plateFormServerImp  implements plateFormServer{
    @Autowired
    Commom commom;

    @Override
    public loginEntity LoginServer(String LoginName){
        loginEntity entity = commom.login(LoginName);
        return entity;
    }
    @Override
    public String CountServer(String sql){
        String count = commom.count(sql);
        return count;
    }
    @Override
    public PageInfo SourseServer(String sql,int pageIndex, int pageNum){
        PageHelper.startPage(pageIndex, pageNum);
        List<fileEntity> Entity = commom.SourseListMapper( sql);
        PageInfo page = new PageInfo(Entity);
        return  page;
    }

    @Override
    public PageInfo EmployeeServer(String sql,int pageIndex, int pageNum){
        PageHelper.startPage(pageIndex, pageNum);
        List<loginEntity> Entity = commom.EmployeeListMapper(sql);
        PageInfo page = new PageInfo(Entity);
        return  page;
    }


    @Override
    public int  UpdateOneFile(fileEntity fileEntity){
        String type = commom.count("select type from plateform_typeConf where name = '"+fileEntity.getType()+"'");
        fileEntity.setType(type);
        return   commom.UpdateOneFile(fileEntity);
    }

    @Override
    public int  UpdateOneEmployee(loginEntity loginEntity){
        return   commom.UpdateOneEmployee(loginEntity);
    }

    @Override
    public int  addEmployee(loginEntity loginEntity){
        return   commom.addEmployee(loginEntity);
    }
    @Override
    public int  deleteOne(String sql){
        return   commom.deleteOne(sql);
    }


    @Override
    public PageInfo EmployeeRecord(String sql,int pageIndex, int pageNum){
        PageHelper.startPage(pageIndex, pageNum);
        List<fileEntity> Entity = commom.SourseListMapper( sql);
        for (int i = 0;i<Entity.size();i++){
            fileEntity ty = Entity.get(i);
            String read = commom.count("select Count(*) as 'read' from plateform_Record where eStatus = 1 and type = 'read' and employeeID = "+ty.getEmployeeID());
            ty.setReadCount(read);
            String share = commom.count("select Count(*) as 'share' from plateform_Record where eStatus = 1 and type = 'share' and employeeID = "+ty.getEmployeeID());
            ty.setShareCount(share);
            String down = commom.count("select Count(*) as 'down' from plateform_Record where eStatus = 1 and type = 'down' and employeeID = "+ty.getEmployeeID());
            ty.setDownCount(down);
            System.out.println(read+"==========="+share+"=========="+down);
            String loginName = commom.count("select loginName from plateform_Employee where eStatus = 1 and employeeID ="+ty.getEmployeeID());
            ty.setLoginName(loginName);

            if (Integer.parseInt(down)> 10 || (Integer.parseInt(share) > 10)){
                ty.setRemind("异常");
            }
        }
        PageInfo page = new PageInfo(Entity);
        return  page;
    }
    @Override
    public int UpdateRead(String sourseID,String username){
        if (username !="未登录"){
            //阅读统计
            fileEntity fileEntity = commom.EditSourceMapper("select * from plateform_file where sourseID = "+sourseID+" and eStatus = 1");
            String ReadCount = fileEntity.getReadCount();
            ReadCount = Integer.toString(Integer.parseInt(ReadCount)+1);
            int result = commom.UpdateSql("update plateform_file set readCount = "+ReadCount+" where sourseID = "+fileEntity.getSourseID());

            //插入阅读记录
            loginEntity loginEntity = commom.EditEmployeeMapper("select * from plateform_Employee where loginName = '"+username+"' and eStatus = 1");
            String type = fileEntity.getFileName().split("_")[0];
            int s = commom.InsertSql("insert into plateform_Record (type,employeeID) value ('"+type+"'"  +","+loginEntity.getEmployeeID()+  ")");

        }else {
            fileEntity fileEntity = commom.EditSourceMapper("select * from plateform_file where sourseID = "+sourseID+" and eStatus = 1");
            String ReadCount = fileEntity.getReadCount();
            int result = commom.UpdateSql("update plateform_file set readCount = "+ReadCount+" where sourseID = "+fileEntity.getSourseID());

        }
        return 1;
    }


    @Override
    public PageInfo selectType(){
        PageHelper.startPage(1,1000);
        List <fileEntity> list =commom.typeConf();
        PageInfo pageInfo = new PageInfo(list);
        return  pageInfo;
    }

}
